/**
 * 系统资源查看js
 */
var sysResourceLook={
	startEvent:function(){
		//设置只读
		sysResourceLook.gerAllResource();
		sysResourceLook.showInfoById();
	},
	
	gerAllResource:function(){
		//获取所有资源
		var url ='mgr/SysResourceCtrl/getSysResourceList';
		var params={
			'curPage':0,
			'pageSize':9999
		};
		var cb = function(data){
			sysResourceLook.dataCache=data.result;
		};
		sendApiNoLoadSync(url, params, cb, 'POST');
	},
	
	showInfoById:function(){
		sysResourceLook.gerAllResource();
		
		var lokId=art.dialog.data('lokId');  //修改id
		
		var url = 'mgr/SysResourceCtrl/getSysResourceById';
		var params ={
				'id':lokId
		};
		var cb = function(data){
			if(data.resultCode==200){
				var ret=data.result;
				
				for(var i in ret){
					var text=ret[i];
					if(i=='resType'){
						$('#com-addForm select[name="'+i+'"]').val(text);
						continue;
					}else if(i=='descp'){
						$('#com-addForm textarea[name="'+i+'"]').val(text);
						continue;
					}else if(i=='uri'){
						$('#com-addForm textarea[name="'+i+'"]').val(text);
						continue;
					}
					
					$('#com-addForm input[name="'+i+'"]').val(text);
				}
				sysResourceLook.showTreeData();
			}
		};
		sendApiNoLoad(url, params, cb, 'POST');
	},
	
	
	setCheck:function(){
		var zTree = $.fn.zTree.getZTreeObj("selSysResourceTree"),
		py = "",
		sy = "s",
		pn = "",
		sn = "s",
		type = { "Y":py + sy, "N":pn + sn};
		zTree.setting.check.chkboxType = type;
	},
	
	showTreeData:function(){
		var setting = {
			check: {
				enable: true,
				chkStyle: "radio",
				radioType: "all"
			},
			data: {
				simpleData: {
					enable: true
				}
			},
			view: {
				nameIsHTML: true
			}
		};
		var zNodes = [];
		var dataCache=sysResourceLook.dataCache;
		
		var parentId=$('#com-addForm input[name="parentId"]').val();
		for (var i = 0; i < dataCache.length; i++) {
			zNodes[i]={
				id:dataCache[i].id,
				name:dataCache[i].name,
				pId:dataCache[i].parentId
			};
			if(parentId==dataCache[i].id){
				zNodes[i].checked=true;
			};
			if(i==0){
				zNodes[i].open=true;
			}
		}
		$.fn.zTree.init($("#selSysResourceTree"), setting, zNodes);
		sysResourceLook.setCheck();
		$("#py").bind("change", sysResourceLook.setCheck());
		$("#sy").bind("change", sysResourceLook.setCheck());
		$("#pn").bind("change",	sysResourceLook.setCheck());
		$("#sn").bind("change",	sysResourceLook.setCheck());
	},
	
	//选择返回事件
	getSelData:function(){
		var zTree = $.fn.zTree.getZTreeObj("selSysResourceTree");
		var nodes = zTree.getCheckedNodes();  // 取得所有选中的
		var len=nodes.length;
		if(len!=1){
			return null;
		}
		return nodes[0].id;
	},
	
	init:function(){
		sysResourceLook.startEvent();
		$("form input").prop("readonly", true);
		$("form input").prop("disabled", true);
		
		$("form select").prop("readonly", true);
		$("form select").prop("disabled", true);
		
		$("form textarea").prop("readonly", true);
		$("form textarea").prop("disabled", true);
	}
}
$(document).ready(function() {
	sysResourceLook.init();
});
